package com.fuce.one.deploy.controller.admin;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.fuce.one.deploy.common.base.AjaxResult;
import com.fuce.one.deploy.common.base.BaseController;
import com.fuce.one.deploy.model.WebApp;
import com.fuce.one.deploy.util.TomcatUtil;

import cn.hutool.core.util.URLUtil;
import io.swagger.annotations.Api;
import org.springframework.web.multipart.MultipartFile;

/**
 * tomcatController
 *
 * @author fuce
 * @ClassName: TomcatController
 * @date 2020-03-02 00:11
 */
@Api(value = "TomcatController")
@Controller
@RequestMapping("/TomcatController")
public class TomcatController extends BaseController {

	private String prefix = "admin/tomcat";

	/**
	 * 项目 首页
	 *
	 * @param map
	 * @return
	 * @author fuce
	 * @Date 2020年3月2日 上午12:10:11
	 */
	@GetMapping("/view")
	public String view(ModelMap map) {
		return prefix + "/list";
	}
	
	/**
	 * tomcat项目列表
	 * @param map
	 * @return
	 * @author fuce
	 * @Date 2020年3月4日 上午2:09:18
	 */
	@PostMapping("/list")
	@ResponseBody
	public Object list(ModelMap map) {
		// 获取组名
		ArrayList<WebApp> arrayList = TomcatUtil.getTomcatWebAppData();
		Map<String, Object> map2 = new HashMap<String, Object>();
		map2.put("total", arrayList.size());
		map2.put("rows", arrayList);
		return map2;
	}
	
	/**
	 * 更新单个文件跳转页面
	 * @param map
	 * @return
	 * @author fuce
	 * @Date 2020年3月4日 上午2:21:34
	 */
	@GetMapping("/putView")
	public String putVie(ModelMap map) {
		return prefix + "/putView";
	}
	
	/**
	 * 停止某个项目
	 * @param projectName
	 * @param tomcatName
	 * @return
	 * @throws IOException
	 * @author fuce
	 * @Date 2020年3月4日 上午2:09:07
	 */
	@PutMapping("/stopProject/{projectName}/{tomcatName}")
	@ResponseBody
	public AjaxResult stopProject(@PathVariable("projectName") String projectName,@PathVariable("tomcatName") String tomcatName) throws IOException {
		boolean b = TomcatUtil.stopWebApp(projectName, tomcatName);
		if (b) {
			return AjaxResult.success("停止成功");
		} else {
			return AjaxResult.error("停止失败");
		}
	}
	
	/**
	 * 启动某个项目
	 * @param projectName
	 * @param tomcatName
	 * @return
	 * @throws IOException
	 * @author fuce
	 * @Date 2020年3月4日 上午2:10:03
	 */
	@PutMapping("/startProject/{projectName}/{tomcatName}")
	@ResponseBody
	public AjaxResult startProject(@PathVariable("projectName") String projectName,@PathVariable("tomcatName") String tomcatName) throws IOException {
		boolean b = TomcatUtil.startWebApp(projectName, tomcatName);
		if (b) {
			return AjaxResult.success("启动成功");
		} else {
			return AjaxResult.error("启动失败");
		}
	}
	
	/**
	 * 部署某个项目
	 * @param projectName
	 * @param tomcatName
	 * @return
	 * @throws IOException
	 * @author fuce
	 * @Date 2020年3月4日 上午2:10:13
	 */
	@PutMapping("/deployWebApp/{projectName}/{tomcatName}")
	@ResponseBody
	public AjaxResult deployWebApp(@PathVariable("projectName") String projectName,@PathVariable("tomcatName") String tomcatName) throws IOException {
		boolean b = TomcatUtil.deployWebApp(projectName, tomcatName);
		if (b) {
			return AjaxResult.success("部署成功");
		} else {
			return AjaxResult.error("部署失败");
		}
	}
	
	/**
	 * 取消部署某个项目
	 * @param projectName
	 * @param tomcatName
	 * @return
	 * @throws IOException
	 * @author fuce
	 * @Date 2020年3月4日 上午2:10:25
	 */
	@PutMapping("/undeployWebApp/{projectName}/{tomcatName}")
	@ResponseBody
	public AjaxResult undeployWebApp(@PathVariable("projectName") String projectName,@PathVariable("tomcatName") String tomcatName) throws IOException {
		boolean b = TomcatUtil.undeployWebApp(projectName, tomcatName);
		if (b) {
			return AjaxResult.success("取消部署成功");
		} else {
			return AjaxResult.error("取消部署失败");
		}
	}
	
	/**
	 * 重启某个项目
	 * @param projectName
	 * @param tomcatName
	 * @return
	 * @throws IOException
	 * @author fuce
	 * @Date 2020年3月4日 上午2:10:36
	 */
	@PutMapping("/reloadWebApp/{projectName}/{tomcatName}")
	@ResponseBody
	public AjaxResult reloadWebApp(@PathVariable("projectName") String projectName,@PathVariable("tomcatName") String tomcatName) throws IOException {
		boolean b = TomcatUtil.reloadWebApp(projectName, tomcatName);
		if (b) {
			return AjaxResult.success("重启动成功");
		} else {
			return AjaxResult.error("重启动失败");
		}
	}

	/**
	 * 获取指定文件加下的所有文件以及文件夹名称
	 * @param projectName
	 * @param tomcatName
	 * @return
	 */
	@GetMapping("/getDirAndFilePath/{projectName}/{tomcatName}")
	@ResponseBody
	public AjaxResult getDirAndFilePath(@PathVariable("projectName") String projectName,
										@PathVariable("tomcatName") String tomcatName){
		return TomcatUtil.getDirAndFilePath(projectName, tomcatName);
	}

	/**
	 * 更新文件
	 * @return
	 * @throws IOException
	 * @author fuce
	 * @Date 2020年3月4日 上午2:10:36
	 */
	@PostMapping("/updateFiles")
	@ResponseBody
	public AjaxResult updateFiles(MultipartFile file,HttpServletRequest request) throws IOException {
		//boolean b = TomcatUtil.updateFiles(file,projectUrl, tomcatName);
		String tomcatName=URLUtil.decode(request.getHeader("tomcatName"), "UTF-8");
		String url=URLUtil.decode(request.getHeader("url"), "UTF-8");
		String projectUrl=URLUtil.decode(request.getHeader("projectUrl"), "UTF-8");
		System.out.println(tomcatName);
		System.out.println(url);
		System.out.println(projectUrl);
		boolean b = TomcatUtil.updateFiles(file,"\\"+url, tomcatName);
		if(b) {
			return AjaxResult.success("更新文件成功");
		} else {
			return AjaxResult.error("更新文件失败");
		}
	}


}
